home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 November / november_2001.iso / Browsers / Netscape 6.1 / browser.xpi / bin / chrome / comm.jar / content / editor / EdImageProps.js < prev    next >
Encoding:
JavaScript  |  2001-06-28  |  21.5 KB  |  669 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Pete Collins
  22.  *   Brian King
  23.  *   Ben Goodger
  24.  */
  25.  
  26. var insertNew     = true;
  27. var insertNewIMap = true;
  28. var wasEnableAll  = false;
  29. var constrainOn = false;
  30. // Note used in current version, but these are set correctly
  31. //  and could be used to reset width and height used for constrain ratio
  32. var constrainWidth  = 0;
  33. var constrainHeight = 0;
  34. var imageElement;
  35. var imageMap = 0;
  36. var canRemoveImageMap = false;
  37. var imageMapDisabled = false;
  38. var dialog;
  39. var globalMap;
  40. var doAltTextError = true;
  41. var actualWidth = "";
  42. var gOriginalSrc = "";
  43. var actualHeight = "";
  44.  
  45. // These must correspond to values in EditorDialog.css for each theme
  46. // (unfortunately, setting "style" attribute here doesn't work!)
  47. var gPreviewImageWidth = 80;
  48. var gPreviewImageHeight = 50;
  49. var StartupCalled = false;
  50.  
  51. // dialog initialization code
  52.  
  53. function Startup()
  54. {
  55.   //XXX Very weird! When calling this with an existing image,
  56.   //    we get called twice. That causes dialog layout
  57.   //    to explode to fullscreen!
  58.   if (StartupCalled)
  59.   {
  60.     dump("*** CALLING IMAGE DIALOG Startup() AGAIN! ***\n");
  61.     return;
  62.   }
  63.   StartupCalled = true;
  64.  
  65.   if (!InitEditorShell())
  66.     return;
  67.  
  68.   doSetOKCancel(onOK, onCancel);
  69.  
  70.   // Create dialog object to store controls for easy access
  71.   dialog = new Object;
  72.  
  73.   dialog.srcInput          = document.getElementById( "srcInput" );
  74.   dialog.altTextInput      = document.getElementById( "altTextInput" );
  75.   dialog.MoreFewerButton   = document.getElementById( "MoreFewerButton" );
  76.   dialog.AdvancedEditButton = document.getElementById( "AdvancedEditButton" );
  77.   dialog.AdvancedEditButton2 = document.getElementById( "AdvancedEditButton2" );
  78.   dialog.MoreSection       = document.getElementById( "MoreSection" );
  79.   dialog.customSizeRadio   = document.getElementById( "customSizeRadio" );
  80.   dialog.actualSizeRadio = document.getElementById( "actualSizeRadio" );
  81.   dialog.constrainCheckbox = document.getElementById( "constrainCheckbox" );
  82.   dialog.widthInput        = document.getElementById( "widthInput" );
  83.   dialog.heightInput       = document.getElementById( "heightInput" );
  84.   dialog.widthUnitsMenulist   = document.getElementById( "widthUnitsMenulist" );
  85.   dialog.heightUnitsMenulist  = document.getElementById( "heightUnitsMenulist" );
  86.   dialog.imagelrInput      = document.getElementById( "imageleftrightInput" );
  87.   dialog.imagetbInput      = document.getElementById( "imagetopbottomInput" );
  88.   dialog.border            = document.getElementById( "border" );
  89.   dialog.alignTypeSelect   = document.getElementById( "alignTypeSelect" );
  90.   dialog.ImageHolder       = document.getElementById( "preview-image-holder" );
  91.   dialog.PreviewWidth      = document.getElementById( "PreviewWidth" );
  92.   dialog.PreviewHeight     = document.getElementById( "PreviewHeight" );
  93.   dialog.PreviewSize       = document.getElementById( "PreviewSize" );
  94.  
  95.   // Get a single selected image element
  96.   var tagName = "img"
  97.   imageElement = editorShell.GetSelectedElement(tagName);
  98.  
  99.   if (imageElement)
  100.   {
  101.     // We found an element and don't need to insert one
  102.     insertNew = false;
  103.     actualWidth  = imageElement.naturalWidth;
  104.     actualHeight = imageElement.naturalHeight;
  105.   }
  106.   else
  107.   {
  108.     insertNew = true;
  109.  
  110.     // We don't have an element selected,
  111.     //  so create one with default attributes
  112.  
  113.     imageElement = editorShell.CreateElementWithDefaults(tagName);
  114.     if( !imageElement )
  115.     {
  116.       dump("Failed to get selected element or create a new one!\n");
  117.       window.close();
  118.       return;
  119.     }
  120.   }
  121.  
  122.   // Make a copy to use for AdvancedEdit
  123.   globalElement = imageElement.cloneNode(false);
  124.  
  125.  
  126.   // Get image map for image
  127.   var usemap = globalElement.getAttribute("usemap");
  128.   if (usemap)
  129.   {
  130.     var mapname = usemap.substring(1, usemap.length);
  131.     var mapCollection = editorShell.editorDocument.getElementsByName(mapname);
  132.     if (mapCollection[0] != null)
  133.     {
  134.       imageMap = mapCollection[0];
  135.       globalMap = imageMap;
  136.       canRemoveImageMap = true;
  137.       insertNewIMap = false;
  138.     }
  139.     else
  140.     {
  141.       insertNewIMap = true;
  142.       globalMap = null;
  143.     }
  144.   }
  145.   else
  146.   {
  147.     insertNewIMap = true;
  148.     globalMap = null;
  149.   }
  150.   InitDialog();
  151.  
  152.   // Save initial source URL
  153.   gOriginalSrc = dialog.srcInput.value;
  154.  
  155.   // By default turn constrain on, but both width and height must be in pixels
  156.   dialog.constrainCheckbox.checked =
  157.     dialog.widthUnitsMenulist.selectedIndex == 0 &&
  158.     dialog.heightUnitsMenulist.selectedIndex == 0;
  159.  
  160.   // Set SeeMore bool to the OPPOSITE of the current state,
  161.   //   which is automatically saved by using the 'persist="more"'
  162.   //   attribute on the MoreFewerButton button
  163.   //   onMoreFewer will toggle the state and redraw the dialog
  164.   SeeMore = (dialog.MoreFewerButton.getAttribute("more") != "1");
  165.  
  166.   // Initialize widgets with image attributes in the case where the entire dialog isn't visible
  167.   onMoreFewer();  // this call will initialize all widgets if entire dialog is visible
  168.  
  169.   SetTextboxFocus(dialog.srcInput);
  170.  
  171.   SetWindowLocation();
  172. }
  173.  
  174. // Set dialog widgets with attribute data
  175. // We get them from globalElement copy so this can be used
  176. //   by AdvancedEdit(), which is shared by all property dialogs
  177. function InitDialog()
  178. {
  179.   // Set the controls to the image's attributes
  180.  
  181.   var str = globalElement.getAttribute("src");
  182.   if (str)
  183.   {
  184.     dialog.srcInput.value = str;
  185.     GetImageFromURL();
  186.   }
  187.  
  188.   str = globalElement.getAttribute("alt");
  189.   if (str)
  190.     dialog.altTextInput.value = str;
  191.  
  192.   // setup the height and width widgets
  193.   var width = InitPixelOrPercentMenulist(globalElement,
  194.                     insertNew ? null : imageElement,
  195.                     "width", "widthUnitsMenulist", gPixel);
  196.   var height = InitPixelOrPercentMenulist(globalElement,
  197.                     insertNew ? null : imageElement,
  198.                     "height", "heightUnitsMenulist", gPixel);
  199.  
  200.   // Set actual radio button if both set values are the same as actual
  201.   if (actualWidth && actualHeight)
  202.     dialog.actualSizeRadio.checked = (width == actualWidth) && (height == actualHeight);
  203.   else if ( !(width || height) )
  204.     dialog.actualSizeRadio.checked = true;
  205.  
  206.   if (!dialog.actualSizeRadio.checked)
  207.     dialog.customSizeRadio.checked = true;
  208.  
  209.   dialog.widthInput.value  = constrainWidth = width ? width : (actualWidth ? actualWidth : "");
  210.   dialog.heightInput.value = constrainHeight = height ? height : (actualHeight ? actualHeight : "");
  211.  
  212.   // set spacing editfields
  213.   dialog.imagelrInput.value = globalElement.getAttribute("hspace");
  214.   dialog.imagetbInput.value = globalElement.getAttribute("vspace");
  215.   dialog.border.value       = globalElement.getAttribute("border");
  216.  
  217.   // Get alignment setting
  218.   var align = globalElement.getAttribute("align");
  219.   if (align) {
  220.     align = align.toLowerCase();
  221.   }
  222.   var imgClass;
  223.   var textID;
  224.   switch ( align )
  225.   {
  226.     case "top":
  227.     case "center":
  228.     case "right":
  229.     case "left":
  230.       dialog.alignTypeSelect.value = align;
  231.       break;
  232.     default:  // Default or "bottom"
  233.       dialog.alignTypeSelect.value = "bottom";
  234.   }
  235.  
  236.   // we want to force an update so initialize "wasEnableAll" to be the opposite of what the actual state is
  237.   wasEnableAll = !IsValidImage(dialog.srcInput.value);
  238.   doOverallEnabling();
  239.   doDimensionEnabling();
  240. }
  241.  
  242. function chooseFile()
  243. {
  244.   // Get a local file, converted into URL format
  245.   var fileName = GetLocalFileURL("img");
  246.   if (fileName)
  247.   {
  248.     dialog.srcInput.value = fileName;
  249.     doOverallEnabling();
  250.   }
  251.   GetImageFromURL();
  252.  
  253.   // Put focus into the input field
  254.   SetTextboxFocus(dialog.srcInput);
  255. }
  256.  
  257. function PreviewImageLoaded()
  258. {
  259.   if (dialog.PreviewImage)
  260.   {
  261.     // Image loading has completed -- we can get actual width
  262.     actualWidth  = dialog.PreviewImage.naturalWidth;
  263.     actualHeight = dialog.PreviewImage.naturalHeight;
  264.  
  265.     if (actualWidth && actualHeight)
  266.     {
  267.       // Use actual size or scale to fit preview if either dimension is too large
  268.       var width = actualWidth;
  269.       var height = actualHeight;
  270.       if (actualWidth > gPreviewImageWidth)
  271.       {
  272.           width = gPreviewImageWidth;
  273.           height = actualHeight * (gPreviewImageWidth / actualWidth);
  274.       }
  275.       if (height > gPreviewImageHeight)
  276.       {
  277.         height = gPreviewImageHeight;
  278.         width = actualWidth * (gPreviewImageHeight / actualHeight);
  279.       }
  280.       dialog.PreviewImage.width = width;
  281.       dialog.PreviewImage.height = height;
  282.  
  283.       dialog.PreviewWidth.setAttribute("value", actualWidth);
  284.       dialog.PreviewHeight.setAttribute("value", actualHeight);
  285.  
  286.       dialog.PreviewSize.setAttribute("collapsed", "false");
  287.       dialog.ImageHolder.setAttribute("collapsed", "false");
  288.  
  289.       // Use values as start for constrain proportions
  290.     }
  291.  
  292.     if (dialog.actualSizeRadio.checked)
  293.       SetActualSize();
  294.   }
  295. }
  296.  
  297. function GetImageFromURL()
  298. {
  299.   dialog.PreviewSize.setAttribute("collapsed", "true");
  300.  
  301.   var imageSrc = dialog.srcInput.value;
  302.   if (imageSrc) imageSrc = imageSrc.trimString();
  303.   if (!imageSrc) return;
  304.  
  305.   if (IsValidImage(imageSrc))
  306.   {
  307.     try {
  308.       // Remove the image URL from image cache so it loads fresh
  309.       //  (if we don't do this, loads after the first will always use image cache
  310.       //   and we won't see image edit changes or be able to get actual width and height)
  311.  
  312.       var uri = Components.classes["@mozilla.org/network/standard-url;1"]
  313.                     .createInstance(Components.interfaces.nsIURI);
  314.       uri.spec = imageSrc;
  315.  
  316.       var imgCacheService = Components.classes["@mozilla.org/image/cache;1"].getService();
  317.       var imgCache = imgCacheService.QueryInterface(Components.interfaces.imgICache);
  318.     
  319.       // This returns error if image wasn't in the cache; ignore that
  320.       imgCache.removeEntry(uri);
  321.  
  322.     } catch(e) {}
  323.  
  324.     if(dialog.PreviewImage)
  325.       removeEventListener("load", PreviewImageLoaded, true);
  326.  
  327.     if (dialog.ImageHolder.firstChild)
  328.       dialog.ImageHolder.removeChild(dialog.ImageHolder.firstChild);
  329.       
  330.     dialog.PreviewImage = document.createElementNS("http://www.w3.org/1999/xhtml", "html:img");
  331.     if(dialog.PreviewImage)
  332.     {
  333.       dialog.ImageHolder.appendChild(dialog.PreviewImage);
  334.       dialog.PreviewImage.addEventListener("load", PreviewImageLoaded, true);
  335.       dialog.PreviewImage.src = imageSrc;
  336.     }
  337.   }
  338. }
  339.  
  340. function SetActualSize()
  341. {
  342.   dialog.widthInput.value = actualWidth ? actualWidth : "";
  343.   dialog.widthUnitsMenulist.selectedIndex = 0;
  344.   dialog.heightInput.value = actualHeight ? actualHeight : "";
  345.   dialog.heightUnitsMenulist.selectedIndex = 0;
  346.   doDimensionEnabling();
  347. }
  348.  
  349. function ChangeImageSrc()
  350. {
  351.   GetImageFromURL();
  352.   doOverallEnabling();
  353. }
  354.  
  355. // This overrides the default onMoreFewer in EdDialogCommon.js
  356. function onMoreFewer()
  357. {
  358.   if (SeeMore)
  359.   {
  360.     dialog.MoreSection.setAttribute("collapsed","true");
  361.     dialog.MoreFewerButton.setAttribute("label", GetString("MoreProperties"));
  362.     dialog.MoreFewerButton.setAttribute("more","0");
  363.     SeeMore = false;
  364.     // Show the "Advanced Edit" button on same line as "More Properties"
  365.     dialog.AdvancedEditButton.setAttribute("collapsed","false");
  366.     dialog.AdvancedEditButton2.setAttribute("collapsed","true");
  367.     // Weird caret appearing when we collapse, so force focus to URL textbox
  368.     dialog.srcInput.focus();
  369.   }
  370.   else
  371.   {
  372.     dialog.MoreSection.setAttribute("collapsed","false");
  373.     dialog.MoreFewerButton.setAttribute("label", GetString("FewerProperties"));
  374.     dialog.MoreFewerButton.setAttribute("more","1");
  375.     SeeMore = true;
  376.  
  377.     // Show the "Advanced Edit" button at bottom
  378.     dialog.AdvancedEditButton.setAttribute("collapsed","true");
  379.     dialog.AdvancedEditButton2.setAttribute("collapsed","false");
  380.   }
  381.   window.sizeToContent();
  382. }
  383.  
  384. function doDimensionEnabling()
  385. {
  386.   // Enabled only if "Custom" is checked
  387.   var enable = (dialog.customSizeRadio.checked);
  388.  
  389.   // BUG 74145: After input field is disabled,
  390.   //   setting it enabled causes blinking caret to appear
  391.   //   even though focus isn't set to it.
  392.   SetElementEnabledById( "heightInput", enable );
  393.   SetElementEnabledById( "heightLabel", enable );
  394.   SetElementEnabledById( "heightUnitsMenulist", enable );
  395.  
  396.   SetElementEnabledById( "widthInput", enable );
  397.   SetElementEnabledById( "widthLabel", enable);
  398.   SetElementEnabledById( "widthUnitsMenulist", enable );
  399.  
  400.   var constrainEnable = enable
  401.          && ( dialog.widthUnitsMenulist.selectedIndex == 0 )
  402.          && ( dialog.heightUnitsMenulist.selectedIndex == 0 );
  403.  
  404.   SetElementEnabledById( "constrainCheckbox", constrainEnable );
  405. }
  406.  
  407. function doOverallEnabling()
  408. {
  409.   var canEnableOk = IsValidImage(dialog.srcInput.value);
  410.   if ( wasEnableAll == canEnableOk )
  411.     return;
  412.  
  413.   wasEnableAll = canEnableOk;
  414.  
  415.   SetElementEnabledById("ok", canEnableOk );
  416.  
  417.   SetElementEnabledById( "imagemapLabel",  canEnableOk );
  418.   //TODO: Restore when Image Map editor is finished
  419.   //SetElementEnabledById( "editImageMap",   canEnableOk );
  420.   SetElementEnabledById( "removeImageMap", canRemoveImageMap);
  421. }
  422.  
  423. function ToggleConstrain()
  424. {
  425.   // If just turned on, save the current width and height as basis for constrain ratio
  426.   // Thus clicking on/off lets user say "Use these values as aspect ration"
  427.   if (dialog.constrainCheckbox.checked && !dialog.constrainCheckbox.disabled
  428.      && (dialog.widthUnitsMenulist.selectedIndex == 0)
  429.      && (dialog.heightUnitsMenulist.selectedIndex == 0))
  430.   {
  431.     constrainWidth = Number(dialog.widthInput.value.trimString());
  432.     constrainHeight = Number(dialog.heightInput.value.trimString());
  433.   }
  434. }
  435.  
  436. function constrainProportions( srcID, destID )
  437. {
  438.   var srcElement = document.getElementById ( srcID );
  439.   if ( !srcElement )
  440.     return;
  441.  
  442.   var destElement = document.getElementById( destID );
  443.   if ( !destElement )
  444.     return;
  445.  
  446.   // always force an integer (whether we are constraining or not)
  447.   forceInteger( srcID );
  448.  
  449.   if (!actualWidth || !actualHeight ||
  450.       !(dialog.constrainCheckbox.checked && !dialog.constrainCheckbox.disabled))
  451.     return;
  452.  
  453.   // double-check that neither width nor height is in percent mode; bail if so!
  454.   if ( (dialog.widthUnitsMenulist.selectedIndex != 0)
  455.      || (dialog.heightUnitsMenulist.selectedIndex != 0) )
  456.     return;
  457.  
  458.   // This always uses the actual width and height ratios
  459.   // which is kind of funky if you change one number without the constrain
  460.   // and then turn constrain on and change a number
  461.   // I prefer the old strategy (below) but I can see some merit to this solution
  462.   if (srcID == "widthInput")
  463.     destElement.value = Math.round( srcElement.value * actualHeight / actualWidth );
  464.   else
  465.     destElement.value = Math.round( srcElement.value * actualWidth / actualHeight );
  466.  
  467. /*
  468.   // With this strategy, the width and height ratio
  469.   //   can be reset to whatever the user entered.
  470.   if (srcID == "widthInput")
  471.     destElement.value = Math.round( srcElement.value * constrainHeight / constrainWidth );
  472.   else
  473.     destElement.value = Math.round( srcElement.value * constrainWidth / constrainHeight );
  474. */
  475. }
  476.  
  477. function editImageMap()
  478. {
  479.   // Make a copy to use for image map editor
  480.   if (insertNewIMap)
  481.   {
  482.     imageMap = editorShell.CreateElementWithDefaults("map");
  483.     globalMap = imageMap.cloneNode(true);
  484.   }
  485.   else
  486.     globalMap = imageMap;
  487.  
  488.   window.openDialog("chrome://editor/content/EdImageMap.xul", "_blank", "chrome,close,titlebar,modal", globalElement, globalMap);
  489. }
  490.  
  491. function removeImageMap()
  492. {
  493.   globalElement.removeAttribute("usemap");
  494.   if (imageMap){
  495.     canRemoveImageMap = false;
  496.     SetElementEnabledById( "removeImageMap", false);
  497.     editorShell.DeleteElement(imageMap);
  498.     insertNewIMap = true;
  499.     globalMap = null;
  500.   }
  501. }
  502.  
  503. // Get data from widgets, validate, and set for the global element
  504. //   accessible to AdvancedEdit() [in EdDialogCommon.js]
  505. function ValidateData()
  506. {
  507.   if ( !IsValidImage(dialog.srcInput.value )) {
  508.     ShowInputErrorMessage(GetString("MissingImageError"));
  509.     return false;
  510.   }
  511.  
  512.   //TODO: WE NEED TO DO SOME URL VALIDATION HERE, E.G.:
  513.   // We must convert to "file:///" or "http://" format else image doesn't load!
  514.   var src = dialog.srcInput.value.trimString();
  515.   globalElement.setAttribute("src", src);
  516.  
  517.   // Note: allow typing spaces,
  518.   // Warn user if empty string just once per dialog session
  519.   //   but don't consider this a failure
  520.   var alt = dialog.altTextInput.value;
  521.   if (doAltTextError && !alt)
  522.   {
  523.     ShowInputErrorMessage(GetString("NoAltText"));
  524.     SetTextboxFocus(dialog.altTextInput);
  525.     doAltTextError = false;
  526.     return false;
  527.   }
  528.   globalElement.setAttribute("alt", alt);
  529.  
  530.   var width = "";
  531.   var height = "";
  532.  
  533.   if (!dialog.actualSizeRadio.checked)
  534.   {
  535.     // Get user values for width and height
  536.     width = ValidateNumber(dialog.widthInput, dialog.widthUnitsMenulist, 1, maxPixels, 
  537.                            globalElement, "width", false, true);
  538.     if (gValidationError)
  539.       return false;
  540.  
  541.     height = ValidateNumber(dialog.heightInput, dialog.heightUnitsMenulist, 1, maxPixels, 
  542.                             globalElement, "height", false, true);
  543.     if (gValidationError)
  544.       return false;
  545.   }
  546.  
  547.   // We always set the width and height attributes, even if same as actual.
  548.   //  This speeds up layout of pages since sizes are known before image is loaded
  549.   if (!width)
  550.     width = actualWidth;
  551.   if (!height)
  552.     height = actualHeight;
  553.  
  554.   // Remove existing width and height only if source changed
  555.   //  and we couldn't obtain actual dimensions
  556.   var srcChanged = (src != gOriginalSrc);
  557.   if (width)
  558.     globalElement.setAttribute("width", width);
  559.   else if (srcChanged)
  560.     globalElement.removeAttribute("width");
  561.  
  562.   if (height)
  563.     globalElement.setAttribute("height", height);
  564.   else if (srcChanged) 
  565.     globalElement.removeAttribute("height");
  566.  
  567.   // spacing attributes
  568.  
  569.   ValidateNumber(dialog.imagelrInput, null, 0, maxPixels, 
  570.                  globalElement, "hspace", false, true, true);
  571.   if (gValidationError)
  572.     return false;
  573.  
  574.   ValidateNumber(dialog.imagetbInput, null, 0, maxPixels, 
  575.                  globalElement, "vspace", false, true);
  576.   if (gValidationError)
  577.     return false;
  578.  
  579.   // note this is deprecated and should be converted to stylesheets
  580.   ValidateNumber(dialog.border, null, 0, maxPixels, 
  581.                  globalElement, "border", false, true);
  582.   if (gValidationError)
  583.     return false;
  584.  
  585.   // Default or setting "bottom" means don't set the attribute
  586.   // Note that the attributes "left" and "right" are opposite
  587.   //  of what we use in the UI, which describes where the TEXT wraps,
  588.   //  not the image location (which is what the HTML describes)
  589.   switch ( dialog.alignTypeSelect.value )
  590.   {
  591.     case "top":
  592.     case "center":
  593.     case "right":
  594.     case "left":
  595.       globalElement.setAttribute( "align", dialog.alignTypeSelect.value );
  596.       break;
  597.     default:
  598.       globalElement.removeAttribute( "align" );
  599.   }
  600.  
  601.   return true;
  602. }
  603.  
  604. function doHelpButton()
  605. {
  606.   openHelp("chrome://help/content/help.xul?image_properties");
  607. }
  608.  
  609. function onOK()
  610. {
  611.   // handle insertion of new image
  612.   if (ValidateData())
  613.   {
  614.     // Assign to map if there is one
  615.     if ( globalMap )
  616.     {
  617.       var mapName = globalMap.getAttribute("name");
  618.       if (mapName != "")
  619.       {
  620.         globalElement.setAttribute("usemap", ("#"+mapName));
  621.         if (globalElement.getAttribute("border") == "")
  622.           globalElement.setAttribute("border", 0);
  623.       }
  624.  
  625.       // Copy or insert image map
  626.       imageMap = globalMap.cloneNode(true);
  627.       if (insertNewIMap)
  628.       {
  629.         try
  630.         {
  631.           editorShell.editorDocument.body.appendChild(imageMap);
  632.         //editorShell.InsertElementAtSelection(imageMap, false);
  633.         }
  634.         catch (e)
  635.         {
  636.           dump("Exception occured in InsertElementAtSelection\n");
  637.         }
  638.       }
  639.     }
  640.  
  641.     // All values are valid - copy to actual element in doc or
  642.     //   element created to insert
  643.     editorShell.CloneAttributes(imageElement, globalElement);
  644.     if (insertNew)
  645.     {
  646.       try {
  647.         // 'true' means delete the selection before inserting
  648.         editorShell.InsertElementAtSelection(imageElement, true);
  649.       } catch (e) {
  650.         dump(e);
  651.       }
  652.     }
  653.  
  654.     // un-comment to see that inserting image maps does not work!
  655.     /*test = editorShell.CreateElementWithDefaults("map");
  656.     test.setAttribute("name", "testing");
  657.     testArea = editorShell.CreateElementWithDefaults("area");
  658.     testArea.setAttribute("shape", "circle");
  659.     testArea.setAttribute("coords", "86,102,52");
  660.     testArea.setAttribute("href", "test");
  661.     test.appendChild(testArea);
  662.     editorShell.InsertElementAtSelection(test, false);*/
  663.  
  664.     SaveWindowLocation();
  665.     return true;
  666.   }
  667.   return false;
  668. }
  669.